<template>
    <div class="product">
    <!-- 按钮开始 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <span style="margin:0 10px">
        <el-button @click='showHandler' type="primary"  plain>添加</el-button>
      </span>
      
      <el-form-item >
        <el-input v-model="formInline.user" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="formInline.region" placeholder="请选择状态">
          <el-option label="正常" value="shanghai"></el-option>
          <el-option label="下架" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="formInline.region" placeholder="请选择栏目">
          <el-option label="家庭保洁" value="shanghai"></el-option>
          <el-option label="衣物护理" value="beijing"></el-option>
          <el-option label="母婴陪护" value="beijing"></el-option>
          <el-option label="家具养护" value="beijing"></el-option>
          <el-option label="家电清洗" value="beijing"></el-option>
          <el-option label="环境整治" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮结束 -->

    <!-- 表格开始 -->
    <el-table :data="products" border style="width: 100%">
      <el-table-column prop="id" label="序号" align="center" width="70"></el-table-column>
      <el-table-column prop="id" label="产品编号" align="center" width="130"></el-table-column>
      <el-table-column
        prop="name"
        label="名称"
        align="center"
        width="130">
      </el-table-column>
      <el-table-column
        label="图片" align='center'>
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <img width="50%" :src="scope.row.photo" >
            <div slot="reference" class="name-wrapper" >
              <img width="60px" :src="scope.row.photo">
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="price"
        label="价格"
        align="center"
        width="120">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        align="center"
        width="120">
      </el-table-column>
      <el-table-column
        prop="category.name"
        label="所属栏目"
        align="center"
        width="130">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        width="150">
        <template slot-scope="scope">
          <el-button @click="editHandler(scope.row)" type="success" plain size="mini">编辑</el-button>
          <el-popconfirm title="确定下架这条产品吗？" @onConfirm="deleteHandler(scope.row.id)">
            <el-button  slot="reference" type="danger" plain size="mini">下架</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->

    <!-- 分页开始 -->
    <div style="float:right;">
    <el-pagination
    layout="prev, pager, next"
    :page-size="list.pageSize"
    :total="total"
    @current-change="changeHandler">
  </el-pagination>
    </div>
    <!-- 分页结束 -->

    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">

      <!-- 表单开始 -->
      <!-- {{ruleForm}}     -->
      <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="name">
          <el-input v-model="ruleForm.num"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="name">
          <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="name">
          <el-input v-model="ruleForm.status"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="name">
          <el-upload
            class="upload-demo"
            action="http://81.69.24.232/pet/jz_upload.php"
            multiple
            :limit="3"
            :on-success='successHandler'>
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <!-- 表单结束 -->

      <div slot="footer" class="dialog-footer">
        <el-button size='mini' @click="dialogFormVisible = false">取 消</el-button>
        <el-button size='mini' type="primary" @click="submitHandler">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->

    </div>
</template>

<script>
import {mapActions, mapState} from 'vuex'
export default {
    data(){
        return{
            list:{
                page:1,
                pageSize:5
            },
            dialogFormVisible:false,
            ruleForm:{},
            title:'添加产品信息',
            input: '',
            formInline: {
            user: '',
            region: '',
        }
        }
    },
    computed:{
        ...mapState('product',['products']),
        ...mapState('product',['total']),
    },

    methods:{
    ...mapActions('product',['findAllProduct','saveProduct','deleteProduct']),
    //分页
    changeHandler(page){
      this.list.page=page;
      this.findAllProduct(this.list)
    },

    //去添加产品信息
    showHandler(){
      this.dialogFormVisible=true
    },

    //提交保存产品信息
    submitHandler(){
      this.saveProduct(this.ruleForm).then(res=>{
        console.log(res,'/////////////////////////');
        if(res.status==200){
          this.dialogFormVisible=false
          this.findAllProduct(this.list)
          this.$message({
            type:'success',
            message:res.message
          })
        }else{
          this.$message({
            type:'error',
            message:res.message
          })
        }
      })
    },

    //编辑修改产品信息
    editHandler(data){
      // console.log(data);
      
      //将当前行的内容，复制给ruleFrom
      this.ruleForm=data
      this.title='编辑产品信息'
      //打开模态框
      this.dialogFormVisible=true
    },

    //删除产品信息
    deleteHandler(id){
      let params={
        id
      }
      
      this.deleteProduct(params).then(res=>{
        if (res.status==200){
          this.findAllProduct(this.list)
          this.$message({
            type:'success',
            message:res.message
          })
        }else{
          this.$message({
            type:'error',
            message:res.message
          })
        }
      })
    },

  //文件上传成功时
    successHandler(file){
      // console.log(file);
      //文件上传成功，将拿到的图片地址，保存到我们栏目信息的对象中（ruleFrom）
      this.ruleForm.icon=file 
    },
    onSubmit() {
        console.log('submit!');
    }
  },

    created(){
    this.findAllProduct(this.list)
  }
}

</script>
